Completed
Push — master ( 434e73...271bd5 )
by
unknown
02:13
created

attributes.js ➔ getAll   F

Complexity

Conditions 12
Paths 1152

Size

Total Lines 44

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 12
c 1
b 0
f 0
nc 1152
dl 0
loc 44
rs 2.7855
nop 2

How to fix   Complexity   

Complexity

Complex classes like attributes.js ➔ getAll often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
import {
2
  Hooks
3
} from '../../'
4
5
/**
6
* Get All attributes from a Abe tag
7
* @return {Object} parsed attributes
8
*/
9
export function getAll(str, json) {
10
  str = Hooks.instance.trigger('beforeAbeAttributes', str, json)
11
12
  //This regex analyzes all attributes of a Abe tag 
13
  var re = /\b([a-z][a-z0-9\-]*)\s*=\s*("([^"]+)"|'([^']+)'|(\S+))/ig
14
  
15
  var attrs = {
16
    autocomplete: null,
17
    desc: '',
18
    display: null,
19
    editable: true,
20
    key: '',
21
    'max-length': null,
22
    'min-length': 0,
23
    order: 0,
24
    prefill: false,
25
    'prefill-quantity': null,
26
    reload: false,
27
    required: false,
28
    source: null,
29
    tab: 'default',
30
    type: 'text',
31
    value: '',
32
    file: '',
33
    visible: true
34
  }
35
  
36
  for (var match; match = re.exec(str); ){
37
    attrs[match[1]] = match[3] || match[4] || match[5]
38
  }
39
40
  attrs.sourceString = attrs.source
41
  attrs.source = (typeof attrs.source !== 'undefined' && attrs.source !== null && attrs.source !== '')? 
42
    ((typeof json['abe_source'] !== 'undefined' && json['abe_source'] !== null && json['abe_source'] !== '')? 
43
      json['abe_source'][attrs.key] : 
44
      null
45
    ) : 
46
    null
47
  attrs.editable = (typeof attrs.editable === 'undefined' || attrs.editable === null || attrs.editable === '' || attrs.editable === 'false') ? false : true
48
49
  attrs = Hooks.instance.trigger('afterAbeAttributes', attrs, str, json)
50
51
  return attrs
52
}
53
54
55
export function sanitizeSourceAttribute(obj, jsonPage){
0 ignored issues
show
Unused Code introduced by
The parameter jsonPage is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
56
  if(typeof obj.sourceString !== 'undefined' && obj.sourceString !== null && obj.sourceString.indexOf('{{') > -1) {
57
    var matches = obj.sourceString.match(/({{[a-zA-Z._]+}})/g)
58
    if(matches !== null) {
59
      Array.prototype.forEach.call(matches, (match) => {
60
        var val = match.replace('{{', '')
61
        val = val.replace('}}', '')
62
        
63
        try {
64
          val = eval('jsonPage.' + val)
0 ignored issues
show
Security Performance introduced by
Calls to eval are slow and potentially dangerous, especially on untrusted code. Please consider whether there is another way to achieve your goal.
Loading history...
65
        }catch(e) {
66
          val = ''
67
        }
68
        obj.sourceString = obj.sourceString.replace(match, val)
69
      })
70
    }
71
  }
72
73
  return obj
74
}